/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.BranchDAO;
import com.cashmate.hibernate.dao.DccrDAO;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.utilities.DCCRDate;
import com.cashmate.utilities.Utils;
import com.opensymphony.xwork2.ActionSupport;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Dean
 */
public class ConfirmGen extends ActionSupport {

    private InputStream inputStream;
    private Logger logger = Logger.getLogger(ConfirmGen.class);
    private final DccrDAO dccrdao = (DccrDAO) ServletActionContext.getServletContext().getAttribute("dccrDAO");
    private final Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private final BranchDAO branchdao = (BranchDAO) ServletActionContext.getServletContext().getAttribute("branchDAO");

    public InputStream getInputStream() {
        return inputStream;
    }

    @Action(value = "/confirm-gen", results = {
        @Result(name = "success", type = "stream", params = {"inputName", "inputStream", "contentType", "text/html"})
    })
    @Override
    public String execute() throws Exception{
        try {
            Object ses_dccrdate = ServletActionContext.getRequest().getSession().getAttribute("dccrdate");
            Object ses_branch = ServletActionContext.getRequest().getSession().getAttribute("dccrbranch");

            if (ses_dccrdate != null && ses_branch != null) {
                //logger.info(ClassUtils.toString(this));
                dccrdao.dccrProcedure(ses_dccrdate.toString(), Integer.parseInt(ses_branch.toString()));
                logger.info("Employee (" + emps.getIdnumber() + ") generated DCCR for " + ses_dccrdate + " at "
                        + branchdao.get(Integer.parseInt(ses_branch.toString())).getBranchname());

                DCCRDate date = new DCCRDate(Utils.stringArrToDateTime(ses_dccrdate.toString().split("/")));
                ServletActionContext.getRequest().getSession().setAttribute("dccrgendate", date);
                ServletActionContext.getRequest().getSession().setAttribute("dccrbranchname", branchdao.get(Integer.parseInt(ses_branch.toString())).getBranchname());

            }
            inputStream = new StringBufferInputStream("<script type=\"text/javascript\">firewarn=false;window.location=\"dccr.jsp\";</script>");
        } catch (Exception ex) {
            //catch whichever exception, get the current session info of the employee and log it.

            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + ex.getLocalizedMessage(), ex);
            } else {
                logger.fatal(ex.getLocalizedMessage(), ex);
            }
            ex.printStackTrace();
            //add action errors this will correspond to <s:actionerror/> in a struts-enabled jsp page.
            addActionError(ex.getLocalizedMessage());

            //if error then chain result to messages.jsp, which will display errors/messages.
            return ERROR;
        }finally{
            BranchDAO.close();
            DccrDAO.close();
        }
        return SUCCESS;
    }
}
